<template>
  <div>
    <n-spin :show="loading" description="请稍候...">
      <n-modal
        v-model:show="showModal"
        :mask-closable="false"
        :show-icon="false"
        preset="dialog"
        transform-origin="center"
        :title="formValue.id > 0 ? '编辑 #' + formValue.id : '添加'"
        :style="{
          width: dialogWidth,
        }"
      >
        <n-scrollbar style="max-height: 87vh" class="pr-5">
          <n-form
            :model="formValue"
            :rules="rules"
            ref="formRef"
            :label-placement="settingStore.isMobile ? 'top' : 'left'"
            :label-width="100"
            class="py-4"
          >
            <n-form-item label="code" path="code">
              <n-input type="textarea" placeholder="code" v-model:value="formValue.code" />
            </n-form-item>

            <n-form-item label="name" path="name">
              <n-input type="textarea" placeholder="name" v-model:value="formValue.name" />
            </n-form-item>

            <n-form-item label="成交额" path="turnover">
              <n-input-number placeholder="请输入成交额" v-model:value="formValue.turnover" />
            </n-form-item>

            <n-form-item label="日期" path="date">
              <n-input type="textarea" placeholder="日期" v-model:value="formValue.date" />
            </n-form-item>

            <n-form-item label="解读" path="desc">
              <n-input type="textarea" placeholder="解读" v-model:value="formValue.desc" />
            </n-form-item>

            <n-form-item label="sell_seat" path="sellSeat">
              <n-input placeholder="请输入sell_seat" v-model:value="formValue.sellSeat" />
            </n-form-item>

            <n-form-item label="buy_seat" path="buySeat">
              <n-input placeholder="请输入buy_seat" v-model:value="formValue.buySeat" />
            </n-form-item>

            <n-form-item label="最新价" path="lastPrice">
              <n-input-number placeholder="请输入最新价" v-model:value="formValue.lastPrice" />
            </n-form-item>

            <n-form-item label="涨跌幅" path="changePercent">
              <n-input-number placeholder="请输入涨跌幅" v-model:value="formValue.changePercent" />
            </n-form-item>

            <n-form-item label="流通市值" path="circulatingMarketCap">
              <n-input-number
                placeholder="请输入流通市值"
                v-model:value="formValue.circulatingMarketCap"
              />
            </n-form-item>

            <n-form-item label="总市值 " path="marketCap">
              <n-input-number placeholder="请输入总市值 " v-model:value="formValue.marketCap" />
            </n-form-item>

            <n-form-item
              label="换手率"
              path="turnoverRate"
            >
              <n-input-number
                placeholder="请输入换手率"
                v-model:value="formValue.turnoverRate"
              />
            </n-form-item>

            <n-form-item
              label="封板资金"
              path="fbzj"
            >
              <n-input-number
                placeholder="请输入封板资金"
                v-model:value="formValue.fbzj"
              />
            </n-form-item>

            <n-form-item label="炸板次数" path="zbcs">
              <n-input-number placeholder="请输入炸板次数" v-model:value="formValue.zbcs" />
            </n-form-item>

            <n-form-item
              label="连板次数"
              path="lbNumber"
            >
              <n-input-number
                placeholder="请输入连板次数"
                v-model:value="formValue.lbNumber"
              />
            </n-form-item>

            <n-form-item label="涨停统计-天数范围" path="ztStatisticsDays">
              <n-input-number
                placeholder="请输入涨停统计-天数范围"
                v-model:value="formValue.ztStatisticsDays"
              />
            </n-form-item>

            <n-form-item
              label="涨停统计-涨停天数"
              path="ztStatisticsNum"
            >
              <n-input-number
                placeholder="请输入涨停统计-涨停天数"
                v-model:value="formValue.ztStatisticsNum"
              />
            </n-form-item>

            <n-form-item label="所属行业" path="industry">
              <n-input type="textarea" placeholder="所属行业" v-model:value="formValue.industry" />
            </n-form-item>

            <n-form-item label="首次封板时间" path="firstFbTime">
              <DatePicker v-model:formValue="formValue.firstFbTime" type="datetime" />
            </n-form-item>

            <n-form-item label="最后封板时间" path="lastFbTime">
              <DatePicker v-model:formValue="formValue.lastFbTime" type="datetime" />
            </n-form-item>
          </n-form>
        </n-scrollbar>
        <template #action>
          <n-space>
            <n-button @click="closeForm">取消</n-button>
            <n-button type="info" :loading="formBtnLoading" @click="confirmForm">确定</n-button>
          </n-space>
        </template>
      </n-modal>
    </n-spin>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { Edit, View } from '@/api/stockZtPool';
  import DatePicker from '@/components/DatePicker/datePicker.vue';
  import { rules, options, State, newState } from './model';
  import { useProjectSettingStore } from '@/store/modules/projectSetting';
  import { useMessage } from 'naive-ui';
  import { adaModalWidth } from '@/utils/hotgo';

  const emit = defineEmits(['reloadTable']);
  const message = useMessage();
  const settingStore = useProjectSettingStore();
  const dialogWidth = ref('75%');
  const loading = ref(false);
  const showModal = ref(false);
  const formValue = ref<State>(newState(null));
  const formRef = ref<any>({});
  const formBtnLoading = ref(false);

  function confirmForm(e) {
    e.preventDefault();
    formBtnLoading.value = true;
    formRef.value.validate((errors) => {
      if (!errors) {
        Edit(formValue.value).then((_res) => {
          message.success('操作成功');
          setTimeout(() => {
            showModal.value = false;
            emit('reloadTable');
          });
        });
      } else {
        message.error('请填写完整信息');
      }
      formBtnLoading.value = false;
    });
  }

  function closeForm() {
    showModal.value = false;
    loading.value = false;
  }

  function openModal(state: State) {
    adaModalWidth(dialogWidth);
    showModal.value = true;
    loading.value = true;

    // 新增
    if (!state || state.id < 1) {
      formValue.value = newState(state);
      return;
    }

    // 编辑
    View({ id: state.id })
      .then((res) => {
        formValue.value = res;
      })
      .finally(() => {
        loading.value = false;
      });
  }

  defineExpose({
    openModal,
  });
</script>

<style lang="less"></style>
